// pages/taskCreate/taskCreate.js
const day=require("../../utils/dayjs.min.js")
Page({


  /**
   * 页面的初始数据
   */
  data: {
    formData: {
    },
    // 0--可用创建 1--不可用
    buttonFail:0,
    date:"2016-09-01",
    time:"12:00",
    rules:[{
      name:'taskName',
      rules:{
        required:true,
        message: "请填写实验名称"
      }
    },
    {
      name:'taskLocation',
      rules:{
        required:true,
        message: "请填写实验地点"
      }
    },
    {
      name:'taskOSL',
      rules:{
        required:true,
        message: "请填写开始时间"
      }
    },{
      name:'taskTDL',
      rules:{
        required:true,
        message: "请填写结束时间"
      }
    },
    {
      name:'taskLimit',
      rules:{
        required:true,
        message: "请填写限制人数"
      }
    }]
  },
  timeStartEvent(time){
    this.setData({
      [`formData.taskOSL`]: time.detail
    })
    console.log("我来了",time.detail)
  },
  timeEndEvent(time){
    this.setData({
      [`formData.taskTDL`]: time.detail
    })
  },
  submitForm(e) {
    let _this=this
    let app=getApp()
    let env=app.globalData.env
    let tableData=this.data.formData
    let classData=this.data.classData
    tableData['teacherId']=app.globalData.user.openid
    tableData['courseId']=classData.data.courseId
    tableData['teacherName']=app.globalData.user.nickName
    console.log(this.data.classData)
    this.selectComponent('#form').validate((valid, errors) => {
      console.log('valid', valid, errors)
      if (!valid) {
        const firstError = Object.keys(errors)
        if (firstError.length) {
          wx.showToast({
            icon: 'none',
            title: errors[firstError[0]].message,
          })
          this.setData({
            error: errors[firstError[0]].message
          })
          console.log(errors[firstError[0]].message)

        }
      } else {
        console.log(this.data.formData)
        wx.showToast({
          title: '校验通过'
        })

        wx.request({
          url: env!=1?'http://localhost:88/api/taskCreate':'https://www.wantstong.com/api/taskCreate',
          method:"POST",
          data:tableData,
          success:res=>{
            console.log(res)
            if(res.data.msg=="success"){
              
                wx.showToast({
                  title: '创建成功',
                })
              _this.setData({
                buttonFail:1
              })
            }
          }
        })

        // wx.navigateBack({
        //   delta: 1,
        // })


      }
    })
  },

  formInputChange(e) {
    const {
      field
    } = e.currentTarget.dataset
    this.setData({
      [`formData.${field}`]: e.detail.value
    })

  },
  selectDayChange(e){
    // let timeStamp =day(e.detail.value).unix()
    this.setData({
      date: e.detail.value,
      [`formData.day`]:e.detail.value
  })
  },
  selectTimeChange(e){
    // console.log(e.detail.value)
    // let timeStamp =day(e.detail.value).format('HH:mm')
    // console.log(timeStamp)
    this.setData({
      time: e.detail.value,
      [`formData.time`]:e.detail.value
  })
  },

  uploadFile(){
    let app=getApp()
    let env=app.globalData.env
    let _this=this
    wx.chooseMessageFile({
      count: 1,
      type:'file',
      success(res){
        const tempFilePaths=res.tempFilePaths
        console.log("选择",res)
        _this.setData({
          fileName:res.tempFiles[0].name,
          [`formData.fileName`]:res.tempFiles[0].name
        
        })
        wx.uploadFile({
          filePath:res.tempFiles[0].path,
          name: 'uploadFile',

          url: env!=1?'http://localhost:88/api/uploadFile':'https://www.wantstong.com/api/uploadFile',
          // url: 'http://localhost:88/api/uploadFile',
          success(res){
            console.log(res)
          }
        })
      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this=this
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on('topData', (res) => {
      _this.setData({
        classData:res
      })
			console.log(res) // my from index page
		})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})